##########################################################
#Experiment Demographics
###########################################################

###Log
my_log <- file("exp_demographics_log.txt")

sink(my_log, append=T, type="output")
sink(my_log, append=T, type="message")

cat(readChar(rstudioapi::getSourceEditorContext()$path, # Writing currently opened R script to file
             file.info(rstudioapi::getSourceEditorContext()$path)$size))

start_time <- Sys.time()
start_time

####Packages
library(rio)
library(tidyverse)

############################
#Exp 1: Both T1 and T2
############################

t1t2 <- import("./Online Appendix A/t1_t2.csv")

###Gender
summarytools::freq(t1t2$gender)

###Age
t1t2 <- t1t2 %>%
  mutate(age_cat = case_when(
    age %in% c(18:24) ~ 1 , 
    age %in% c(25:34) ~ 2 , 
    age %in% c(35:44) ~ 3 , 
    age %in% c(45:54) ~ 4 , 
    age %in% c(55:64) ~ 5 , 
    age >=65  ~ 6 ))

summarytools::freq(t1t2$age_cat)

mean(t1t2$age, na.rm=T)
sd(t1t2$age, na.rm=T)


###Education

t1t2 <- t1t2 %>%
  mutate(educ1 = car::recode(educ_old, '1=1;2=2;3=3;4=3;5=4;6=5;7=5;8=5'))

summarytools::freq(t1t2$educ1)

###Race/Eth

summarytools::freq(t1t2$race_cat)

###Income

median(t1t2$income, na.rm=T)
#=5=40,000 to 49,999

###PID
mean(t1t2$pid, na.rm=T)
sd(t1t2$pid, na.rm=T)

summarytools::freq(t1t2$pid_3)

###Ideology

mean(t1t2$ideol_gen, na.rm=T)
sd(t1t2$ideol_gen, na.rm=T)

t1t2 <- t1t2 %>%
  mutate(ideol3 = case_when(
    ideol_gen %in% c(1:3) ~ 1, 
    ideol_gen == 4 ~ 2, 
    ideol_gen %in% c(5:7) ~ 3
  ))

summarytools::freq(t1t2$ideol3)

############################
#Exp 1: Just T2
############################

exp1 <- import("./Data/exp1_cleaned.csv")

nrow(t1t2)
nrow(exp1)

###Gender
summarytools::freq(exp1$gender)

###Age
exp1 <- exp1 %>%
  mutate(age_cat = case_when(
    age %in% c(18:24) ~ 1 , 
    age %in% c(25:34) ~ 2 , 
    age %in% c(35:44) ~ 3 , 
    age %in% c(45:54) ~ 4 , 
    age %in% c(55:64) ~ 5 , 
    age >=65  ~ 6 ))

summarytools::freq(exp1$age_cat)

mean(exp1$age, na.rm=T)
sd(exp1$age, na.rm=T)


###Education

exp1 <- exp1 %>%
  mutate(educ1 = car::recode(educ_old, '1=1;2=2;3=3;4=3;5=4;6=5;7=5;8=5'))

summarytools::freq(exp1$educ1)

###Race/Eth

summarytools::freq(exp1$race_cat)

###Income

median(exp1$income, na.rm=T)
#=5=40,000 to 49,999

###PID
mean(exp1$pid, na.rm=T)
sd(exp1$pid, na.rm=T)

#Note: Pure independents were not recruited for the T2 survey
summarytools::freq(exp1$pid_3)

###Ideology

mean(exp1$ideol_gen, na.rm=T)
sd(exp1$ideol_gen, na.rm=T)

exp1 <- exp1 %>%
  mutate(ideol3 = case_when(
    ideol_gen %in% c(1:3) ~ 1, 
    ideol_gen == 4 ~ 2, 
    ideol_gen %in% c(5:7) ~ 3
  ))

summarytools::freq(exp1$ideol3)


############################
#Exp 2
############################

exp2 <- import("./Data/exp2_cleaned.csv")

###Gender

summarytools::freq(exp2$gender)

#proportion without the two missing cases: 
1194/2355

###Age
exp2 <- exp2 %>%
  mutate(age_cat = case_when(
    age %in% c(18:24) ~ 1 , 
    age %in% c(25:34) ~ 2 , 
    age %in% c(35:44) ~ 3 , 
    age %in% c(45:54) ~ 4 , 
    age %in% c(55:64) ~ 5 , 
    age >=65  ~ 6 ))

summarytools::freq(exp2$age_cat)

mean(exp2$age, na.rm=T)
sd(exp2$age, na.rm=T)

###Education
summarytools::freq(exp2$educ_trunc)

###Race/Eth
summarytools::freq(exp2$race_cat)

###Income
median(exp2$income, na.rm=T)

###PID
mean(exp2$pid, na.rm=T)
sd(exp2$pid, na.rm=T)

summarytools::freq(exp2$pid_3)

###Ideology
mean(exp2$ideol_symb, na.rm=T)
sd(exp2$ideol_symb, na.rm=T)

summarytools::freq(exp2$ideol_3)


############################
#Exp 3
############################

exp3 <- import("./Data/exp3_cleaned.csv")

###Gender
summarytools::freq(exp3$gender)

###Age
mean(exp3$age, na.rm=T)
sd(exp3$age, na.rm=T)

summarytools::freq(exp3$age_cat)
#adjusting for the "empty string" responses
353/(3077-47)
603/(3077-47)
591/(3077-47)
494/(3077-47)
457/(3077-47)
532/(3077-47)

###Education
summarytools::freq(exp3$educ)
#adjusting fro the "empty string" responses
126/(3077-8)
819/(3077-8)
1178/(3077-8)
613/(3077-8)
336/(3077-8)

###Race/Eth
summarytools::freq(exp3$race_eth)

exp3 <- exp3 %>%
  mutate(race1 = case_when(
    race_eth == "White"  ~  1, 
    race_eth == "African-American"  ~  2, 
    race_eth == "Hispanic"  ~  3, 
    race_eth == "Asian"  ~  4, 
    race_eth %in% c("Native Hawaiian or Pacific Islander", "Other") ~ 5  ))

summarytools::freq(exp3$race1)

###Income
median(exp3$income, na.rm=T)

###PID
mean(exp3$partyid, na.rm=T)
sd(exp3$partyid, na.rm=T)

summarytools::freq(exp3$pid_3)
1377/(3077-2)
1083/(3077-2)
615/(3077-2)

###Ideology
mean(exp3$ideol, na.rm=T)
sd(exp3$ideol, na.rm=T)

exp3 <- exp3 %>%
  mutate(ideol3 = case_when(
    ideol %in% c(1,2,3) ~ 1, 
    ideol == 4 ~ 2, 
    ideol %in% c(5, 6, 7) ~ 3))

summarytools::freq(exp3$ideol3)


end_time <- Sys.time()
end_time


closeAllConnections()
